﻿/***********************************************************************
 * CLR 版本: 4.0.30319.225
 * 工程名称: $projectname$
 * 命名空间: Toulr.Util.Geography
 * 文件名称: SizeDouble
 * 描    述: 
 * 作    者: 18901309129@189.cn
 * 登录用户: hyyu
 * 创建时间: 2011-6-14 18:54:31
 * 创建年份: 2011
 * Update History: 
 ***********************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;

namespace Toulr.Util.Geography
{
    /// <summary>
    /// 表示平面直角坐标系中的尺寸，数值使用Double类型
    /// </summary>
    public struct SizeDouble
    {
        /// <summary>
        /// 用来表示无实际值的Size的实例
        /// </summary>
        public static readonly SizeDouble Empty;

        static SizeDouble()
        {
            Empty = new SizeDouble();
        }

        private double width;
        private double height;

        /// <summary>
        /// 用一个现有的<see cref="Size"/>的值初始化<see cref="SizeDouble"/>值
        /// </summary>
        /// <param name="size"></param>
        public SizeDouble(Size size)
        {
            width = size.Width;
            height = size.Height;
        }

        /// <summary>
        /// 用一个现有的<see cref="SizeF"/>的值初始化<see cref="SizeDouble"/>值
        /// </summary>
        /// <param name="size"></param>
        public SizeDouble(SizeF size)
        {
            width = size.Width;
            height = size.Height;
        }

        /// <summary>
        /// 用一个现有的<see cref="SizeDouble"/>的值初始化<see cref="SizeDouble"/>值
        /// </summary>
        /// <param name="size"></param>
        public SizeDouble(SizeDouble size)
        {
            width = size.Width;
            height = size.Height;
        }

        /// <summary>
        /// 用一个现有的<see cref="PointDouble"/>的值初始化<see cref="SizeDouble"/>值
        /// </summary>
        /// <param name="pt"></param>
        public SizeDouble(PointDouble pt)
        {
            width = pt.X;
            height = pt.Y;
        }

        /// <summary>
        /// 用宽度和高度的值初始化<see cref="SizeDouble"/>值
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public SizeDouble(double width, double height)
        {
            this.width = width;
            this.height = height;
        }

        public static SizeDouble operator +(SizeDouble sz1, SizeDouble sz2)
        {
            return Add(sz1, sz2);
        }

        public static SizeDouble operator -(SizeDouble sz1, SizeDouble sz2)
        {
            return Subtract(sz1, sz2);
        }

        public static bool operator ==(SizeDouble sz1, SizeDouble sz2)
        {
            return ((sz1.Width == sz2.Width) && (sz1.Height == sz2.Height));
        }

        public static bool operator !=(SizeDouble sz1, SizeDouble sz2)
        {
            return !(sz1 == sz2);
        }

        public static explicit operator PointDouble(SizeDouble size)
        {
            return new PointDouble(size.Width, size.Height);
        }

        [Browsable(false)]
        public bool IsEmpty
        {
            get
            {
                return (width == Empty.width) && (height == Empty.height);
            }
        }

        /// <summary>
        /// 获取或设置宽度的值
        /// </summary>
        public double Width
        {
            get { return width; }
            set { width = value; }
        }

        /// <summary>
        /// 获取或设置高度的值
        /// </summary>
        public double Height
        {
            get { return this.height; }
            set { this.height = value; }
        }

        /// <summary>
        /// 将两个<see cref="SizeDouble"/>的宽度和高度分别相加
        /// </summary>
        /// <param name="sz1"></param>
        /// <param name="sz2"></param>
        /// <returns></returns>
        public static SizeDouble Add(SizeDouble sz1, SizeDouble sz2)
        {
            return new SizeDouble(sz1.Width + sz2.Width, sz1.Height + sz2.Height);
        }

        /// <summary>
        /// 从sz1的宽度和高度分别减去sz2的宽度和高度
        /// </summary>
        /// <param name="sz1"></param>
        /// <param name="sz2"></param>
        /// <returns></returns>
        public static SizeDouble Subtract(SizeDouble sz1, SizeDouble sz2)
        {
            return new SizeDouble(sz1.Width - sz2.Width, sz1.Height - sz2.Height);
        }

        /// <summary>
        /// 判断与另一对象是否相等
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            if (!(obj is SizeDouble))
            {
                return false;
            }
            SizeDouble ef = (SizeDouble)obj;
            return (((ef.Width == Width) && (ef.Height == Height))
                && ef.GetType().Equals(GetType()));
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public PointDouble ToPointDouble()
        {
            return (PointDouble)this;
        }

        public Size ToSize()
        {
            return new Size(Convert.ToInt32(width), Convert.ToInt32(height));
        }

        public SizeF ToSizeF()
        {
            return new SizeF(Convert.ToSingle(width), Convert.ToSingle(height));
        }

        public override string ToString()
        {
            return ("{Width=" + width.ToString(CultureInfo.CurrentCulture) + ", Height=" + height.ToString(CultureInfo.CurrentCulture) + "}");
        }

    }
}
